package com.example.gouwu2;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;


public class LoginActivity extends AppCompatActivity {
    private EditText etZhanghao, etPassword;
    private Button btnLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        // 初始化组件
        etZhanghao = findViewById(R.id.l_zhanghao);
        etPassword = findViewById(R.id.l_pass);
        btnLogin = findViewById(R.id.l_login);

        // 检查是否有从注册页面传递过来的账号密码
        Intent intent = getIntent();
        if (intent != null) {
            String account = intent.getStringExtra("account");
            String password = intent.getStringExtra("password");
            if (account != null) etZhanghao.setText(account);
            if (password != null) etPassword.setText(password);
        }

        // 登录按钮点击事件
        btnLogin.setOnClickListener(v -> {
            String inputAccount = etZhanghao.getText().toString().trim();
            String inputPassword = etPassword.getText().toString().trim();

            // 验证输入是否为空
            if (inputAccount.isEmpty() || inputPassword.isEmpty()) {
                Toast.makeText(LoginActivity.this, "账号和密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }


            // 登录成功
            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();

            // 跳转到主页面
            Intent homeIntent = new Intent(LoginActivity.this, HomeActivity.class);
            startActivity(homeIntent);
            finish(); // 关闭登录页面
        });
    }
}